<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/29
 * Time: 14:57
 */

namespace app\api\controller\v1;


use app\api\validate\Count;
use app\api\validate\IDMustBePostiveInt;
use app\lib\exception\ProductException;
use app\api\model\Product as productModel;

/*
 * @商品类
 * */
class Product
{
    /*
     * @得到最新的默认15条商品数据
     * */
    public function getRecent($count = 15)
    {
        (new Count())->goCheck();
        $result = productModel::getMostRecent($count);
        /*collection数据集
            也可在数据库配置文件中设置返回值的类型
        */
        $collection = collection($result);
        if ($collection->isEmpty()){
            throw new ProductException();
        }
        $result = $collection->hidden(['summary']);

        return $result;
    }

    /*
     * @获取指定品类的商品信息
     * */
    public function getAllInCategory($id){
        (new IDMustBePostiveInt())->goCheck();
        $result = productModel::getProductsByCategoryId($id);
        $products = collection($result);
        if ($products->isEmpty()){
            throw new ProductException();
        }
        $products = $products->hidden(['summary']);
        return $products;
    }

    /*
     * @查询商品详情
     */
    public function getOne($id){
        (new IDMustBePostiveInt())->goCheck();
        $product = productModel::getProductDetail($id);

        if (!$product) {
            throw new ProductException();
        }
        return $product;
    }

    /*
     * @删除指定商品
     * */
}